package com.hdy.javachatroomdemo.Config;

import com.aliyun.oss.OSS;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.File;

import com.aliyuncs.auth.DefaultCredentialsProvider;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OSSConfig {
    @Autowired
    private OSSProperties ossProperties;
    @Bean("ossClient")
    public OSS ossClient() throws ClientException {
        CredentialsProvider credentialsProvider=new DefaultCredentialProvider(ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
        ClientBuilderConfiguration clientBuilderConfiguration=new ClientBuilderConfiguration();
        OSS client =OSSClientBuilder.create()
                .endpoint(ossProperties.getEndpoint())
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(ossProperties.getRegion())
                .build();
        return client;
    }
    @PreDestroy
    public void destroy(){
        ossClient().shutdown();
    }
}
